import { login, register, uploadPage, charge, wish, changeInfo, reqMyPostList, reqMyComment, reqdeleteComment, reqMygame, reqMydeposit, reqGame } from '@/api'

const state = {
    userInfo: localStorage.getItem('userInfo'),
    myPost: [],
    myComment: [],
    myOrder: [],
    myDeposit: [],
    myGame: [],
    myWish: []
};
const mutations = {
    GETUSERINFO(state, userInfo) {
        state.userInfo = userInfo
    },
    GETMYPOST(state, myPost) {
        state.myPost = myPost
    },
    GETMYCOMMENT(state, myComment) {
        state.myComment = myComment
    },
    GETORDER(state, myOrder) {
        state.myOrder = myOrder
    },
    GETDEPOSIT(state, myDeposit) {
        state.myDeposit = myDeposit;
    },
    GETGAME(state, myGame) {
        state.myGame = myGame;
    },
    GETMYWISH(state, myWish) {
        state.myWish = myWish;
    }
};
const actions = {
    // 登录
    async userLogin({ commit }, Info) {

        const { username, password } = Info;
        let res = await login({ name: username, password: password });
        if (res.code == 200) {
            commit('GETUSERINFO', res.obj);
            localStorage.setItem('userInfo', JSON.stringify(res.obj))
            return 'ok'
        } else {
            return Promise.reject(new Error('登录失败！请重新输入'));
        }
    },
    // 注册
    async userRegister({ commit }, info) {
        const { username, sex, phone, password } = info;
        let res = await register({ name: username, sex: sex, phone: phone, password: password });
        console.log(res);
    },
    // 上传头像
    async pageUpload({ commit }, userInfo) {
        const { name, page } = userInfo;
        console.log(userInfo)
        let res = await uploadPage({ name: name, page: page })
        if (res.code == 200) {
            localStorage.setItem('userInfo', JSON.stringify(userInfo))
            return res.msg;
        } else
            return Promise.new.Error('上传失败');
    },
    // 充钱
    async addMoney({ commit }, userInfo) {
        const { username, addcount, card } = userInfo;

        let res = await charge({ username: username, addcount: addcount, card: card })
        if (res.code == 200) {
            return 'ok'
        } else {
            return Promise.new.Error('充值失败，请再次输入!')
        }
    },
    // 查看愿望单
    async getWishList({ commit }, username) {
        let res = await wish({ username })
        if (res.code == 200) {
            commit('GETMYWISH', res.data)
            return res.msg;
        } else return Promise.new.Error('获取心愿单列表失败')
    },
    // 修改个人信息
    async editInfo({ commit }, userInfo) {
        let res = await changeInfo(userInfo)
        if (res.code == 200) {
            return 'ok'
        } else {
            return Promise.new.Error('faile');
        }
    },
    // 查看个人发的帖子
    async getMyPost({ commit }, name) {
        let res = await reqMyPostList({ username: name });
        if (res.code == 200) {
            commit('GETMYPOST', res.data)
        } else {
            return Promise.new.Error('faile')
        }
    },
    // 查看所有跟帖
    async getMyComment({ commit }, name) {

        let res = await reqMyComment({ username: name })
        if (res.code == 200) {
            commit('GETMYCOMMENT', res.data)
        } else {
            return Promise.new.Error('faile')
        }
    },
    // 删掉我的跟帖
    async deleteMyComment({ commit }, id) {
        let res = await reqdeleteComment({ id })
        if (res.code == 200) {
            return 'ok'
        } else {
            return Promise.new.Error('faile');

        }
    },
    // 我的订单
    async getOrder({ commit }, name) {
        let res = await reqMygame({ name });
        if (res.code == 200) {
            commit('GETORDER', res.data)
        } else {
            return Promise.new.Error('faile');
        }
    },
    // 我的充值记录
    async getDeposit({ commit }, name) {
        let res = await reqMydeposit({ name });
        if (res.code == 200) {
            commit('GETDEPOSIT', res.data)
        } else {
            return Promise.new.Error('faile');
        }
    },
    // 我的游戏
    async getGame({ commit }, name) {
        let res = await reqGame({ name });
        if (res.code == 200) {
            commit('GETGAME', res.data)
            return 'ok'
        } else {
            return Promise.new.Error('faile');
        }
    }


};
const getters = {};

export default {
    state,
    mutations,
    actions,
    getters
}